package com.bbs2.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.bbs2.entity.ConcernPost;
import com.bbs2.util.DBHelp;
import com.bbs2.util.RowMapper;

public class ConcernPostDao {
	private DBHelp<ConcernPost> db = new DBHelp<ConcernPost>();
	public int save(ConcernPost cp){
		String sql = "INSERT INTO t_concernpost(flag,user_id,post_id) VALUE(?,?,?)";
		return db.executeUpdate(sql, cp.getFlag(),cp.getUserId(),cp.getPostId());
	}
	public ConcernPost checkConcernPost(String userId,String postId){
		String sql = "SELECT flag,user_id,post_id FROM t_concernpost WHERE user_id=? AND post_id=?";
		return db.queryForObject(sql, new ConcernPostMap(), userId,postId);
	}
	public List<ConcernPost> findByUserId(String userid){
		StringBuilder sql = new StringBuilder();
//		sql.append("SELECT c.user_id AS cuid,c.post_id AS cpid,c.flag AS cflag,p.id AS pid,p.title AS title,p.createTime,p.content,p.flag AS pflag,p.clickNum,p.t_board_id,p.t_user_id,u.id AS uid,u.name AS uname,u.password AS PASSWORD,u.email,u.flag AS uflag,u.pic AS pic,u.regTime ");
//		sql.append(" FROM t_concernpost AS c ");
//		sql.append("LEFT JOIN t_post AS p ON p.id=c.post_id ");
//		sql.append("LEFT JOIN t_user AS u ON u.id=c.user_id ");
//		sql.append(" WHERE c.user_id=? AND c.flag=1 ");
		sql.append(" SELECT flag,user_id,post_id FROM t_concernpost WHERE user_id=? AND flag=1");
		return db.queryForList(sql.toString(), new ConcernPostMap(), userid);
	}
	public List<ConcernPost> findByPostId(String postid){
		StringBuilder sql = new StringBuilder();
		sql.append(" SELECT flag,user_id,post_id FROM t_concernpost WHERE post_id=? AND flag=1");
		return db.queryForList(sql.toString(), new ConcernPostMap(), postid);
	}
}
class ConcernPostMap implements RowMapper<ConcernPost>{

	public ConcernPost mapRow(ResultSet rs) throws SQLException {
		ConcernPost cp = new ConcernPost();
		cp.setFlag(rs.getInt("flag"));
		cp.setPostId(rs.getInt("user_id"));
		cp.setUserId(rs.getInt("post_id"));
		
//		Post p = new Post();
//		p.setId(rs.getInt("pid"));
//		p.setTitle(rs.getString("title"));
//		p.setBoardId(rs.getInt("t_board_id"));
//		p.setClickNum(rs.getInt("clickNum"));
//		p.setContent(rs.getString("content"));
//		p.setCreateTime(rs.getString("createTime"));
//		p.setFlag(rs.getInt("pflag"));
//		p.setUserId(rs.getInt("t_user_id"));
//		cp.set
		return cp;
	}
	
	
}